package com.yiwei.jmx.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.allen.library.SuperButton;
import com.yiwei.jmx.R;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.RetrieveByPhoneV3Bean;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.event.FindXueXinSuccessEvent;
import com.yiwei.jmx.util.Dialog;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.UI;
import com.yiwei.jmx.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3XueXinFindByPhoneResetActivity extends BaseActivity {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.btn_wechat)
    ImageView btnWechat;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.btn_send_code)
    TextView btnSendCode;
    @BindView(R.id.edit_pass)
    EditText editPass;
    @BindView(R.id.btn_pass_eye)
    ImageView btnPassEye;
    @BindView(R.id.edit_repass)
    EditText editRepass;
    @BindView(R.id.btn_repass_eye)
    ImageView btnRepassEye;
    @BindView(R.id.linearLayout)
    LinearLayout linearLayout;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.txt_tips)
    SuperButton txtTips;
    private Unbinder unbinder;
    private String mobile;
    private String clst;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xuexin_phone_reset_pass_v3);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        mobile = getIntent().getStringExtra("mobile");
        txtTips.setText("重置密码短信已发送至" + mobile + ",登录用户名" + mobile);
        clst = getIntent().getStringExtra("clst");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }


    public void getRetrieveByPhoneV3(String clst, String smscode, String password, String confirm_pass) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        RetrieveByPhoneV3Bean bean = new RetrieveByPhoneV3Bean();
        bean.clst = clst;
        bean.password = password;
        bean.confirm_pass = confirm_pass;
        bean.smscode = smscode;
        Api.api_service.getRetrieveByPhoneV3(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(model -> {
                    EventBus.getDefault().post(new FindXueXinSuccessEvent());
                    ToastUtil.makeText(this, "修改成功");
                    finish();
                });
    }


    @OnClick({R.id.btn_back, R.id.btn_send_code, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_send_code:
                Dialog.showDefaultDialog(this, "", "收不到验证码？返回上一步继续获取", "取消", "确定", false, new Dialog.DialogDefaultClickListener() {
                    @Override
                    public void confirm() {
                        finish();
                    }

                    @Override
                    public void cancel() {

                    }
                });

                break;
            case R.id.btn_submit:
                if (codeViewInputCheck()) {
                    getRetrieveByPhoneV3(clst, UI.toString(editCode), UI.toString(editPass), UI.toString(editRepass));
                }
                break;
        }
    }

    private boolean codeViewInputCheck() {
        if (TextUtils.isEmpty(UI.toString(editCode))) {
            ToastUtil.makeText(getApplicationContext(), "验证码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (UI.toString(editPass).length() < 6) {
            ToastUtil.makeText(getApplicationContext(), "新密码长度至少6位", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (UI.toString(editRepass).length() < 6) {
            ToastUtil.makeText(getApplicationContext(), "重复密码长度至少6位", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!UI.toString(editPass).equals(UI.toString(editRepass))) {
            ToastUtil.makeText(getApplicationContext(), "两次密码输入不一致", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
